/*
 * @Author: jiu yin
 * @Date: 2024-06-21 16:41:00
 * @LastEditTime: 2024-06-26 16:18:14
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \studio-web-admin\src\view\pico\component\SceneTable.tsx
 * jiu
 */
import { useRef, useState } from "react";
import { application_list_device } from "@/api/home";

import type { ActionType } from "@ant-design/pro-components";
import { ProTable } from "@ant-design/pro-components";
import { ConfigProvider, Image, message } from "antd";
import SceneDeploy from "./SceneDeploy";
import { b_to_mb } from "@/utils";

export default function MapTable(props: any) {
	const { device_real_sn } = props;

	const actionRef = useRef<ActionType>(null);

	const reload = () => {
		actionRef.current && actionRef.current.reload();
	};

	//部署
	let [isModalOpen, setIsModalOpen] = useState(false);
	let [item, setItem] = useState<any>({});

	const columns = [
		{
			dataIndex: "index",
			valueType: "indexBorder",
			width: 48,
		},
		{
			title: "场景信息",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className="font-700 flex">
					<Image width={84} height={56} className="border-rd-10px" src={item.cover_url} />
					<div className="ml-10px ">
						<h5 className="c-#161940">{item.name}</h5>
						<p className="c-#5C5F7A">{item.describe}</p>
					</div>
				</div>
			),
		},
		{
			title: "互动参数",
			dataIndex: "max_num",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className=" text-14px">
					<p className="c-#9e9fb0">
						最大组队成员：<span className="c-#323557">{item.max_num}人</span>
					</p>
					<p className="c-#9e9fb0">
						时长：<span className="c-#323557">{item.exp_duration}min</span>
					</p>
				</div>
			),
		},

		{
			title: "场景大小",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => <p className=" text-14px c-#161940">{b_to_mb(item.size)}MB</p>,
		},
		{
			title: "版本",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className=" text-14px">
					<p className="c-#9e9fb0">
						最新版本：<span className="c-#323557">{item.latest_version}</span>
					</p>
					<p className="c-#9e9fb0">
						上次部署版本：<span className="c-#323557">{item.version || "未安装"}</span>
					</p>
				</div>
			),
		},
		{
			title: "操作",
			valueType: "option",
			key: "option",
			render: (_: any, item: any) => [
				<a key="editable" onClick={() => {
					setItem(item);
					setIsModalOpen(true);
				}}>
					部署其他
				</a>,
			],
		},
	];

	return (
		<section className="ml--24px">
			<ConfigProvider>
				<ProTable
					columns={columns}
					actionRef={actionRef}
					request={async (params, sort, filter) => {
						let { data } = await application_list_device({
							page: params.current,
							limit: params.pageSize,
							...params,
							device_real_sn,
						});
						data.data.forEach((item: any, index: number) => (item.key = index + 1));
						return {
							data: data.data,
							success: true,
							total: data.total,
						};
					}}
					rowKey="key"
					tableAlertRender={false}
					search={false}
					options={false}
					pagination={{
						pageSize: 10,
						hideOnSinglePage: true,
					}}
					dateFormatter="string"
				/>
			</ConfigProvider>
			{isModalOpen && <SceneDeploy isModalOpen={[isModalOpen, setIsModalOpen]} item={item} reload={reload}></SceneDeploy>}
		</section>
	);
}
